package com.itetchologyandenglish.home.controller.Api;

import com.alibaba.fastjson.JSONObject;
import com.itetchologyandenglish.home.config.Log;
import com.itetchologyandenglish.home.enetity.Auth;
import com.itetchologyandenglish.home.enetity.AuthUser;
import com.itetchologyandenglish.home.enetity.Result;
import com.itetchologyandenglish.home.service.AuthService;
import com.itetchologyandenglish.home.service.PageService;
import com.itetchologyandenglish.home.service.RoleService;
import com.itetchologyandenglish.home.service.UserAthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
@Api(tags={"登录和模块维护模块"})
@RequestMapping("/admin")
@RestController
public class AuthUserApiController {
    @Resource
    UserAthService userAthService;
    @Resource
    PageService pageService;
     @Resource
    AuthService authService;
     @Resource
     RoleService roleService;
    @ApiOperation("登录")
    @Log(value="登录-登录")
    @RequestMapping(value="/login",produces = "application/json")
    public Result<AuthUser> dologin(@RequestParam("userName")String username, @RequestParam("password")String password,HttpSession session) {
        AuthUser user=userAthService.loadUserByUsername(username, password);
        System.out.println(user);
        if(user==null){
            return new Result<>(200,"error",null);
        }
        session.removeAttribute("code");
        session.setAttribute("user",user);
        JSONObject object=new JSONObject();
        object.put("user",user);
        return new Result<AuthUser>(200,"success",user);
   }
    @ApiIgnore
    @Log(value="登录-刷新session")
    @RequestMapping("/refreshSession")
	public void refreshSession(HttpSession session) {
		AuthUser user=(AuthUser) session.getAttribute("user");
        System.out.println(user);
		if(user != null){
			session.setAttribute("user", user);
		}else{

			System.out.println("session 取不到值");
		}
	}
    @ApiIgnore
	@Log("登录-获得session信息")
    @RequestMapping(value="/getSessionAdmin",produces = "application/json")
    public Result<List<Auth>> getSessionAdmin(HttpSession session){
        AuthUser user=(AuthUser)session.getAttribute("user");
        List<Auth> authList=pageService.getAuthByRoleId(user.getRoles().getRoleId());
        if(authList==null){
            return new Result(200,"success",null);
        }
        return new Result<List<Auth>>(200,"success",authList);
    }
    @Log("登录-更新使用者密码")
    @RequestMapping(value="/adminUpdatePwd",method = RequestMethod.POST,produces = "application/json")
    public Result adminUpdatePwd(String oldpwd,String newpwd,HttpSession session){
       AuthUser user=(AuthUser)session.getAttribute("user");
        if(oldpwd.equals(user.getPassword())){
            if(pageService.updatePwd(oldpwd,newpwd,user.getUsername())){
                return new Result(200,"success");
            }else{
                return new Result(200,"error");
            }
        }else{
            return new Result(200,"verifyerror");
        }

    }
    @ApiOperation("退出账户")
    @Log("登录-退出账户")
    @RequestMapping("/logout")
    public Result logout(HttpSession session){
        AuthUser user=(AuthUser)session.getAttribute("user");
        System.out.println(user);
        session.removeAttribute("user");
        return new Result(200,"success");
    }
    @ApiOperation("注销账户")
    @Log("登录-注销账户")
    @RequestMapping("/zhuxiao")
    public Result zhuxiao(HttpSession session){
        AuthUser user=(AuthUser)session.getAttribute("user");
        System.out.println(user.getId());
        if(userAthService.updatedelete(user.getId())){
            session.removeAttribute("user");
            return new Result(200,"success");
        }else{
            return new Result(200,"error");
        }
    }
    @ApiOperation("查看模块列表")
    @Log("模块维护-获得模块列表")
    @RequestMapping(value="/AuthList",produces = "application/json")
    public Result<List<Auth>> AuthList()
    {
        return new Result<List<Auth>>(200,"success",authService.list());
    }
    @ApiOperation("配置权限")
    @Log("模块维护-配置权限")
    @RequestMapping(value="/upRoleAuth",produces = "application/json")
    public Result upRoleAuth(@RequestParam("roleId")Integer roleId,@RequestParam("authIds")Integer[] authIds){
        authService.delAuthRole(roleId);
        int i=authService.addAuthRole(roleId,authIds);
        return new Result(200,"成功设置"+i);
    }
    @ApiOperation("查看角色所拥有的模块")
    @Log("模块维护-根据角色ID搜索模块")
    @RequestMapping(value="/searchAuth",produces = "application/json")
    public Result<List<Integer>> searchAuth(Integer id){
        List<Integer> auths=authService.getAuthByRoleId(id);
        return new Result<List<Integer>>(200,"success",auths);
    }
    @ApiIgnore
    @Log("模块维护-根据ID搜索模块")
    @RequestMapping(value="/searchAuths",produces = "application/json")
    public Result<Auth> searchAuths(Integer id){
        Auth auth=authService.getAuthById(id);
        return new Result<Auth>(200,"success",auth);
    }
    @ApiOperation("更新模块")
    @Log("模块维护-根据模块ID等信息更新模块")
    @RequestMapping(value="/Authupdate",produces = "application/json")
    public Result updateAuth(@RequestParam("id")Integer id,@RequestParam("authName") String authName,@RequestParam("authLabel") String authLabel,@RequestParam("content") String content,@RequestParam("type")Integer type){
        Auth auth=new Auth().setId(id).setAuthName(authName).setAuthLabel(authLabel).setContent(content).setType(type);
        System.out.println(auth);
        if(!authService.updateAuth(auth)) {
            return new Result(200, "更新错误");
        }
        return new Result(200,"更新成功");
    }
    @ApiOperation("删除模块")
    @Log("模块维护-根据ID删除模块")
    @RequestMapping(value="/removeAuth",produces = "application/json")
    public Result removeAuth(@RequestParam("id")Integer id){
        if(authService.removeAuth(id)){
            return new Result(200,"删除成功");
        }else{
            return new Result(200,"删除失败");
        }

    }
    @ApiOperation("添加角色")
    @Log("角色维护-添加角色")
    @RequestMapping(value="/roleAdd",produces = "application/json")
    public Result addRole(@RequestParam("roleId")Integer roleId,@RequestParam("roleName")String roleName){
        if(roleService.addRole(roleId,roleName)){
            return new Result(200,"添加成功");
        }
        return new Result(200,"添加失败");
    }
    @ApiOperation("添加模块")
    @Log("模块维护-添加模块")
    @RequestMapping(value="/authAdd",produces = "application/json")
    public Result authAdd(@RequestParam("authName")String authName,@RequestParam("authLabel")String authLabel,@RequestParam("content")String content,@RequestParam("path")String path,@RequestParam("type")Integer type) {
        Auth auth = new Auth().setAuthName(authName).setAuthLabel(authLabel).setContent(content).setPath(path).setType(type);
        if (authService.addAuth(auth)) {
            return new Result(200, "添加成功");
        } else {
            return new Result(200, "添加失败");
        }
    }
}
